package com.region.integral.common_user.obtain.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.common_user.obtain.dao.IIntegralOnline;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.common_user.pojo.CUIEntity;
import com.region.integral.common_user.xml.Regulation;
import com.region.integral.common_user.xml.dto.Online;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.CommonUserLogUtil;
import com.region.integral.util.XMLRegulationUtil;
@Component("iOnline")
public class IntegralOnlineImpl extends BaseHibernate implements IIntegralOnline {

	@Override
	public void online(int userid,IntegralData integralData)throws IntegralException {
		CUIEntity cuiEntity = getCUIEntity(userid);
		if (cuiEntity != null) {
			Regulation regulation = XMLRegulationUtil.unmarshaller();
			List<Online> onlines = regulation.getOnline();
			for (Online online : onlines) {
					int currIntegral = cuiEntity.getCurrIntegral();
					int maxIntegral = cuiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ online.getIntegral();
					cuiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						cuiEntity.setMaxIntegral(totalIntegral);
					}
					integralData.setSuccee(true);
					hibernateTemplate.update(cuiEntity);
					hibernateTemplate.save(CommonUserLogUtil.log(userid, online.getIntegral(), "online"));
			}
		}
	}
}
